// pages/mine/userinfo/repasssword/index.js
var app=getApp();
import WxValidate from '../../../../utils/WxValidate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
  },
  initValidate() {
    const rules = {      
      oldpassword: {
        required: true,
        minlength: 6,
        maxlength: 16
      },
      newpassword: {
        required: true,
        minlength: 6,
        maxlength: 16
      },
      renewpassword:{
        required: true,
        equalTo: 'newpassword'
      }
    }
    const message = {      
      oldpassword: {
        required: '请输入旧密码',
        minlength: '旧密码长度不能少于6位',
        maxlength: '旧密码长度不能超过16位'
      },
      newpassword: {
        required: '请输入新密码',
        minlength: '新密码长度不能少于6位',
        maxlength: '新密码长度不能超过16位'
      },
      renewpassword:{
        required: '请输入确认新密码',
        equalTo: '确认新密码不一致'
      }
    }
    this.WxValidate = new WxValidate(rules, message);   
  },
  showModal(error) {
    wx.showToast({
      title: error.msg,
      duration: 2000,
      icon: 'none'
    });
  },
  submitData: function (events) {
    var userInfo = app.globalData.userInfo;
    const params = events.detail.value;
    console.log(params)
    if (params != undefined) {
      if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList[0]
        this.showModal(error);
        return;
      }
    }
    params.id = userInfo.id
    app.paiRequest('/app/user/updateUserInfo', params).then(function (res) {
      wx.navigateBack({
        delta: 1
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})